package com.lyp.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lyp.jsonbean.CarOrdersDetailsBean;
import com.lyp.jsonbean.LoginBean;
import com.lyp.jsonbean.NofenpeiBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//未分配车内任务详情
public class NofenpeiCarDetailsActivity extends Activity implements View.OnClickListener{

    private NofenpeiBean.DataBean mBean;
    private List<CarOrdersDetailsBean.DataBean> mList = new ArrayList<>();
    private Button mBtnOrder;
    private TextView mName, mPhone, mXing, mPingpai, mAge, mPai, mOrder, mTime;
    private ImageView mBack;
    private LinearLayout mCar;
    private LoginBean.DataBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);
        init();
    }

    private void init() {
        bean = (LoginBean.DataBean) getIntent().getExtras().get("databeans");
        mBean = (NofenpeiBean.DataBean) getIntent().getExtras().get("databean");
        mBack =(ImageView) findViewById(R.id.carback);
        mName = (TextView) findViewById(R.id.carname);
        mPhone = (TextView) findViewById(R.id.carphone);
        mXing = (TextView) findViewById(R.id.carxing);
        mPingpai = (TextView) findViewById(R.id.carpingpai);
        mAge = (TextView) findViewById(R.id.carage);
        mPai = (TextView) findViewById(R.id.carpai);
        mOrder = (TextView) findViewById(R.id.carorder);
        mTime = (TextView) findViewById(R.id.cartime);

        mCar = (LinearLayout) findViewById(R.id.ll_car);

        mBtnOrder = (Button) findViewById(R.id.btn_order_allot);
        mBtnOrder.setVisibility(View.VISIBLE);
        mBack.setOnClickListener(this);
        mCar.setVisibility(View.VISIBLE);
        mBtnOrder.setOnClickListener(this);

        car(mBean.getO_id());
    }

    //车内详情(我的任务)
    public void car(String o_id) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YangxixiApi api = retrofit.create(YangxixiApi.class);
        Call<CarOrdersDetailsBean> call = api.getCarOrders(o_id);
        call.enqueue(new Callback<CarOrdersDetailsBean>() {
            @Override
            public void onResponse(Call<CarOrdersDetailsBean> call, final Response<CarOrdersDetailsBean> response) {
                if (response.body().getResult() == 1) {
                    NofenpeiCarDetailsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mList.addAll(response.body().getData());
                            mName.setText(mList.get(0).getUser_name());
                            mPhone.setText(mList.get(0).getU_phone());
                            mXing.setText(mList.get(0).getCar_type());
                            mPingpai.setText(mList.get(0).getUser_car_type());
                            mAge.setText(mList.get(0).getCar_age());
                            mPai.setText(mList.get(0).getLicense_number());
                            mOrder.setText(mList.get(0).getOrder_number());
                            mTime.setText(mList.get(0).getPay_time());
                        }
                    });
                }
            }
            @Override
            public void onFailure(Call<CarOrdersDetailsBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_order_allot:
                Intent intent = new Intent(this, IndependentActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("ids", mBean.getO_id());
                bundle.putSerializable("databean", bean);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
                break;
            case R.id.carback:
                finish();
                break;
        }
    }
}
